package map;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import map.graph.MapGraph;

import com.taxi.server.carsearch.graph.Node;
import com.taxi.server.carsearch.graph.Way;
import com.taxi.server.carsearch.mathutils.Line;

@SuppressWarnings("serial")
public class MapGraphDrawer extends JFrame {

	private JPanel contentPane;
	private int width;
	private int height;

	private MapGraph graph;
	private LinkedList<Way> additionalWays = new LinkedList<Way>();
	private LinkedList<Line> additionalLines = new LinkedList<Line>();

	/**
	 * Create the frame.
	 */
	public MapGraphDrawer(MapGraph graph) {
		this.graph = graph;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 600, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

	}

	@Override
	public void paint(Graphics g) {
		contentPane.setBackground(Color.white);
		width = contentPane.getWidth();
		height = contentPane.getHeight();

		for (Way way : graph.getWays()) {
			drawWay(g, way);
		}
		for (Node n : graph.getNodes().values()) {
			drawNode(g, n);

		}
		for (Way way : additionalWays) {
			Point p1 = absToRel(way.getNode1().getLon(), way.getNode1()
					.getLat());
			Point p2 = absToRel(way.getNode2().getLon(), way.getNode2()
					.getLat());
			g.setColor(Color.blue);
			g.drawLine(p1.x, p1.y, p2.x, p2.y);
		}
		for (Line line : additionalLines) {
			Point p1 = absToRel(line.p1.x, line.p1.y);
			Point p2 = absToRel(line.p2.x, line.p2.y);
			g.setColor(Color.RED);
			g.drawLine(p1.x, p1.y, p2.x, p2.y);
		}
	}

	public void addWay(Way w) {
		additionalWays.add(w);
	}

	public void addLine(Line l) {
		additionalLines.add(l);
	}

	private void drawWay(Graphics g, Way way) {
		Point p1 = absToRel(way.getNode1().getLon(), way.getNode1().getLat());
		Point p2 = absToRel(way.getNode2().getLon(), way.getNode2().getLat());
		int speed = way.getSpeed();
		if (speed <= 20) {
			g.setColor(Color.RED);
		} else if (speed <= 40) {
			g.setColor(Color.ORANGE);
		} else if (speed <= 60) {
			g.setColor(Color.YELLOW);

		} else {
			g.setColor(Color.GREEN);
		}
		g.drawLine(p1.x, p1.y, p2.x, p2.y);
	}

	private void drawNode(Graphics g, Node node) {
		Point p = absToRel(node.getLon(), node.getLat());
		g.setColor(Color.black);
		g.drawOval(p.x, p.y, 1, 1);
		// g.drawString(String.valueOf(node.getId()), p.x, p.y);
	}

	private Point absToRel(double lon, double lat) {
		Point p = new Point();
		p.x = (int) Math
				.round((width * (lon - graph.minlon + 0.0005) / (graph.maxlon
						- graph.minlon + 0.001)));
		p.y = height
				- (int) Math
						.round((height * (lat - graph.minlat + 0.0005) / (graph.maxlat
								- graph.minlat + 0.001)));
		return p;

	}

}
